package com.mine.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mine.pojo.Cart;

import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.service.CartService;

@Controller
public class CartController {

	@Autowired
	CartService cartService;
	//////////////////////购物车主键id与(商品id+用户id)是一一对应的关系：既是一个用户加入一个商品到购物车产生一个唯一值//////////////////////////////
	//添加一件商品到购物车
	@RequestMapping(value="/cart/addOneToCart/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result addOneGoodsToCart(@PathVariable String token, @RequestBody Cart cart){
		if (cart==null||token==null||cart.getShopId()==null||cart.getChooseNum()==null||cart.getGoodsId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		if (cart.getSelectedSpecifications()==null) {
			cart.setSelectedSpecifications("");
		}
		if (cart.getChooseNum()==0) {
			return Result.error("加入购物车中的商品数量不能为0！");
		}
		cart.setCreateTime(new Date());
		Result result = cartService.addOneGoodsToCart(token,cart);
		return result;
	}
	
	//更新购物车的某个商品数量 如果cart.getChooseNum()==0 那么说明要删除这个商品
	@RequestMapping(value="/cart/updateOneGoodsOnCart/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateOneGoodsOnCart(@PathVariable String token, @RequestBody Cart cart){
		
		if (cart==null||token==null||cart.getChooseNum()==null||cart.getShopId()==null||cart.getGoodsId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		if (cart.getId()==null&&cart.getGoodsId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		if(cart.getSelectedSpecifications()==null){
			cart.setSelectedSpecifications("");
		}
		
		//至少得有一个id、goodsId
		Result result = cartService.updateOneGoodsOnCart(token,cart);
		return result;
	}
	
	//查询某个用户的购物车
	@RequestMapping(value="/cart/selectCartList/{token}")
	@ResponseBody
	public Result selectCartList(@PathVariable String token){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = cartService.selectCartList(token);
		return result;
	}
	
	//删除购物车中的多个商品
	@RequestMapping(value="/cart/deleteGoodsOnCart/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result deleteGoodsListOnCart(@PathVariable String token, @RequestBody List<Long> carts){
		if (carts==null||token==null||carts.size()==0) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
//		for (int i = 0; i < carts.size(); i++) {
//			System.out.println(carts.get(i));
//		}
//		System.out.println(carts.toString());
		Result result = cartService.deleteGoodsListOnCart(token,carts);
		
		return result;
//		return null;
	}
	
}
